#include "mut.h"
#include "mutbound.h"

LPDIRECT3DINDEXBUFFER9 g_boxib = 0;
int g_boxcnt = 0;

HRESULT CMUTBoundingBox::Create(){/*
	LPDIRECT3DDEVICE9 pDevice = MUTState()->GetDevice();
	if ( !pDevice ) return E_FAIL;
	g_boxcnt++;
	HRESULT hr = S_OK;
	if ( FAILED( hr = pDevice->CreateVertexBuffer( 8*sizeof( D3DXVECTOR3 ), 0, D3DFVF_XYZ, D3DPOOL_MANAGED, &vb, NULL ) ) ){
		MUTDXErrorTrace( hr, L"box vb failed" );
		return E_FAIL;
	}
	if ( !g_boxib ){
		if ( FAILED( hr = pDevice->CreateIndexBuffer( 12*3*sizeof(WORD), 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_boxib, NULL ) ) ){
			MUTDXErrorTrace( hr, L"box ib failed" );
			return E_FAIL;
		}
		WORD* indices = 0;
		g_boxib->Lock( 0, 0, (void**)&indices, 0 );
		WORD indicesTmp[] = {
			0,1,2,
			2,1,3,
			2,3,4,
			4,3,5,
			4,5,6,
			6,5,7,
			6,7,0,
			0,7,1,
			1,7,3,
			3,7,5,
			2,4,0,
			0,4,6
		};
		for ( int i = 0; i < 36; i++ )
			indices[i] = indicesTmp[i];
		g_boxib->Unlock();
	}*/
	return S_OK;
}

void CMUTBoundingBox::Release(){/*
	if ( vb )
		vb->Release();
	vb = 0;
	g_boxcnt--;
	if ( g_boxcnt<=0 ){
		if ( g_boxib )
			g_boxib->Release();
		g_boxib = 0;
	}*/
}

bool CMUTBoundingBox::InsideFrustum( D3DXPLANE* p ){
	D3DXVECTOR3 vCenter = ( minPos + maxPos ) / 2;
	float r = D3DXVec3Length( &( minPos - vCenter ) );
	for ( int i = 0; i < 6; i++ ){
		if ( D3DXPlaneDotCoord( p+i, &vCenter ) < -r )
			return false;
	}
	return true;
}

void CMUTBoundingBox::SetupBounding(){/*
	D3DXVECTOR3 *vertices = 0;
	vb->Lock( 0, 0, (void**)&vertices, 0 );
	vertices[0] = minPos;
	vertices[1] = D3DXVECTOR3( minPos.x, maxPos.y, minPos.z );
	vertices[2] = D3DXVECTOR3( maxPos.x, minPos.y, minPos.z );
	vertices[3] = D3DXVECTOR3( maxPos.x, maxPos.y, minPos.z );
	vertices[4] = D3DXVECTOR3( maxPos.x, minPos.y, maxPos.z );
	vertices[5] = maxPos;
	vertices[6] = D3DXVECTOR3( minPos.x, minPos.y, maxPos.z );
	vertices[7] = D3DXVECTOR3( minPos.x, maxPos.y, maxPos.z );
	vb->Unlock();*/
}

void CMUTBoundingBox::Render(){/*
	LPDIRECT3DDEVICE9 pDevice = MUTState()->GetDevice();
	if ( !pDevice ) return;
	pDevice->SetStreamSource( 0, vb, 0, sizeof( D3DXVECTOR3 ) );
	pDevice->SetIndices( g_boxib );
	pDevice->SetFVF( D3DFVF_XYZ );
	pDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );*/
}